<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-10-3
 * Time: 下午11:32
 */

class SearchController extends Controller {

    public function init() {
        parent::init();
    }


    public function indexAction(){
        define('ITEM_PER_PAGE', 10);

        $region = $this->_request->getParam('region');
        $region=empty($region) ? '全部' :htmlspecialchars(trim($region) , ENT_QUOTES ,'utf-8');
        $brand_name= $this->_request->getParam('brand_name');
        $brand_name=empty($brand_name) ? '' :htmlspecialchars(trim($brand_name) , ENT_QUOTES ,'utf-8');
        $cn= $this->_request->getParam('cn');
        $cn=empty($cn) ? '' :htmlspecialchars(trim($cn) , ENT_QUOTES ,'utf-8');
        $area= $this->_request->getParam('area');
        $area=empty($area) ? '' :htmlspecialchars(trim($area) , ENT_QUOTES ,'utf-8');
        $keyword= $this->_request->getParam('keyword');
        $keyword=empty($keyword) ? '' :htmlspecialchars(trim($keyword) , ENT_QUOTES ,'utf-8');
        $orderbyNum= $this->_request->getParam('orderbyNum','ASC');
        $orderbyNum=empty($orderbyNum) ? '' :htmlspecialchars(trim($orderbyNum) , ENT_QUOTES ,'utf-8');
        $orderByPrice= $this->_request->getParam('orderByPrice','ASC');
        $orderByPrice=empty($orderByPrice) ? '' :htmlspecialchars(trim($orderByPrice) , ENT_QUOTES ,'utf-8');
        $minPrice= $this->_request->getParam('minPrice');
        $minPrice=empty($minPrice) ? '' :htmlspecialchars(trim($minPrice) , ENT_QUOTES ,'utf-8');
        $maxPrice= $this->_request->getParam('maxPrice');
        $maxPrice=empty($maxPrice) ? '' :htmlspecialchars(trim($maxPrice) , ENT_QUOTES ,'utf-8');
        $type= $this->_request->getParam('type');
        $type=empty($type) ? '' :htmlspecialchars(trim($type) , ENT_QUOTES ,'utf-8');
        $currency_code= $this->_request->getParam('currency_code');
        $currency_code=empty($currency_code) ? 'RMB' :htmlspecialchars(trim($currency_code) , ENT_QUOTES ,'utf-8');
        $page= $this->_request->getParam('page');
        $page  = empty($page) ? 1 : (int) $page;

        $val =array(
            'region'  => $region,
            'currency_code' => $currency_code,
            'brand_name'  => $brand_name,
            'product' => $brand_name,
            'cate_name' => $cn,
            'area'     => $area,
            'keyword' => $keyword,
            'pageSize'   => ITEM_PER_PAGE,
            'page'   => $page,
            'type'   => $type,
            'orderByNum'=>$orderbyNum,
            'orderByPrice'=>$orderByPrice,
            'minPrice'=>$minPrice,
            'maxPrice'=>$maxPrice,
        );
        $url = _HTTP_CLOUD_HOST."/api/getprice/getprice";

        $pro = Tools::https_request($url,$val);
        $prolist = json_decode($pro,true);

        $totalItems   = $prolist['totalSize'];
        $offset = 0;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems)
        {
            $page   = floor($totalItems / ITEM_PER_PAGE);
            $offset         = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0)
        {
            $offset         = 0;
            $page   = 1;
        }

        $getlists = $prolist['data'];

        $qoure=array();
        foreach($getlists as $key=>$var){
            $qoure[$key]['data_type'] ='1';
            $qoure[$key]['id'] = $var['id'];
            $qoure[$key]['price'] = $var['price'];
            $qoure[$key]['create_time']= $var['create_time'];
            $qoure[$key]['remain'] = $var['remain'];
            $qoure[$key]['status'] =$var['status'];
            $qoure[$key]['type'] = $var['type'];
            $qoure[$key]['num'] = $var['num'];
            $qoure[$key]['product_id'] = $var['product_id'];
            $qoure[$key]['delivery_time'] = $var['delivery_time'];
            $qoure[$key]['delivery_area'] = $var['delivery_area'];
            $qoure[$key]['delivery_method'] = $var['delivery_method'];
            $qoure[$key]['deposit_rate'] = $var['deposit_rate'];
            $qoure[$key]['tray_type'] = $var['tray_type'];
            $qoure[$key]['short_name'] = empty($var['short_name']) ? '' :$var['short_name'];
            $qoure[$key]['product_name'] = $var['product_name'];
            $qoure[$key]['brand_name'] = $var['brand_name'];
            $qoure[$key]['conpany'] = "塑米城";
            $qoure[$key]['realname'] = $var['realname'];
            $qoure[$key]['qq'] = empty($var['qq']) ? '':$var['qq'];
            $qoure[$key]['mobile'] = $var['mobile'];
            $qoure[$key]['wxpic'] = $var['wxpic'];
            $qoure[$key]['personpic'] = $var['personpic'];
            $qoure[$key]['service_name'] = $var['service_name'];
            $qoure[$key]['vice_uom_name'] = $var['vice_uom_name'];
            $qoure[$key]['cate_name']= $var['cate_name'];
        }



        //开始查询出供应的信息
        $quota = new QuotationModel();

        $where ="type='outer'";
        $orderby ="update_time desc";

        $totalItems = $quota->getCountByWhere($where);


        $lists = $quota->getPageListByWhere($page,ITEM_PER_PAGE, $where,$orderby);



        if(!empty($lists)){
            foreach($lists as $key=>$var){
                $qoure_s[$key]['data_type'] ='2';
                $qoure_s[$key]['id'] = $var['id'];
                $qoure_s[$key]['price'] = $var['price'];
                $qoure_s[$key]['create_time']= $var['create_time'];
                $qoure_s[$key]['remain'] = $var['num'];
                $qoure_s[$key]['status'] = $var['status'];
                $qoure_s[$key]['type'] = 'party';
                $qoure_s[$key]['num'] = $var['num'];
                $qoure_s[$key]['product_id'] = '';
                $qoure_s[$key]['delivery_time'] = $var['delivery_time'];
                $qoure_s[$key]['delivery_area'] = $var['delivery_area'];
                $qoure_s[$key]['delivery_method'] = '';
                $qoure_s[$key]['deposit_rate'] = '';
                $qoure_s[$key]['tray_type'] = '';
                $qoure_s[$key]['short_name'] = '';
                $qoure_s[$key]['product_name'] = $var['product_name'];
                $qoure_s[$key]['brand_name'] = $var['brand_name'];
                $qoure_s[$key]['conpany'] = $var['business_name'];
                $qoure_s[$key]['letter']  =substr($var['business_remarks'],0,3);
                $qoure_s[$key]['realname'] = $var['contacts'];
                $qoure_s[$key]['qq'] = '';
                $qoure_s[$key]['mobile'] = $var['mobile'];
                $qoure_s[$key]['wxpic'] = '';
                $qoure_s[$key]['personpic'] ='';
                $qoure_s[$key]['service_name'] ='现货';
                $qoure_s[$key]['vice_uom_name'] ='';
                $qoure_s[$key]['cate_name']= $var['cate_name'];
            }
        }



        if(!empty($qoure) && !empty($qoure_s)){
            $data = array_merge($qoure,$qoure_s);
        }else{
            if(empty($qoure) && empty($qoure_s)){
                $data= array();
            }else{
                if(empty($qoure)){
                    $data = $qoure_s;
                }else{
                    $data = $qoure;
                }
            }
        }


        $time_order = array();
        foreach ($data as $var) {
            $time_order[] = $var['create_time'];
        }

        array_multisort($time_order, SORT_DESC, $data);

        if(!empty($data)){
            foreach($data as $key=>$val){
                $data[$key]['create_time'] =Tools::time2Units(time () - strtotime ( $val ['create_time'] )) . "前";
            }
        }

        $this->_view->data =$data;
        $this->_view->keyword = $keyword;
        $this->_view->totalItems = $totalItems;
        $this->_view->page =$page;
        $this->_view->getlists =$getlists;
        $this->_view->orderbyNum = $orderbyNum;
        $this->_view->orderByPrice = $orderByPrice;
    }

}